<template>
	<view>
		<view class="flex-col items-start section_1">
			<view class="group_5">
				<text class="text_2">可分享：</text>
				<text class="text_3">{{num}}</text>
				<text class="text_4">名额</text>
				<!-- <text class="text_5" style="padding-left: 200rpx">添加球友</text> -->
			</view>
			<view @click="addlist" style="position: absolute; top: 22rpx; right: 30rpx;color: #2f75fa;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;">
				添加球友
			</view>
		</view>
		<view >
			<view class="goods-detail" v-for="(item,index) in list" :key="index">
				<view class="detail-left">
					<view class="goods-left">
						<checkbox-group @change="selected(item)">
							<label>
								<checkbox class="selected" color="#555555" :checked="checked" /><text></text>
							</label>
						</checkbox-group>
						<image :src="item.avatar" style="width: 150rpx;height: 140rpx;"></image>
					</view>
					<view class="size">
						<text style="font-size: 25rpx;">{{item.friend_name}}</text>
						<text class="goods-price">{{item.friend_phone}}</text>
					</view>
				</view>
				<view class="detail-right">
					<text class="subtract" @click="reduce(item)">-</text>
					<text class="num">{{item.nums}}</text>
					<text @click="add(item)" class="add">+</text>
				</view>
			</view>
		</view>
		<view class="end">
			<view class="end-left">
				<checkbox-group @change="selectgoods()">
					<label>
						<checkbox :checked="allchecked" />全选
					</label>
				</checkbox-group>
				<view>
					总计：<text style="color: #f00;font-weight: bold;">{{totalPrice}}</text>
				</view>
			</view>
			<view class="end-right"  @click="shareFriend">
				确定
			</view>


		</view>
	</view>
</template>

<script>
	const venues = require("@/api/venues/venues.js");
	export default {
		data() {
			return {
				allchecked: false,
				checked: false,
				list: [],
				num:"",
			}
		},
		computed: {
			checked: {
				get() {
					return this.value;
				},
				set(val) {
					this.$emit('input', val);
				}
			},
			// totalNum() {
			// 	let totalNum = 0;
			// 	this.list.map(item => {
			// 		// item.flag ? totalNum += item.num : totalNum += 0
			// 		item.checked ? totalNum += item.nums : totalNum += 0
			// 	})
			// 	return totalNum
			// },

			totalPrice() {
				let totalPrice = 0;
				this.list.map(item => {
					// item.flag ? totalPrice += item.num * item.price : totalPrice += 0
					item.checked ? totalPrice += item.nums * 1 : totalPrice += 0
				})
				return totalPrice
			}
		},
		onLoad(options) {
			this.id = options.id
			this.court_no = options.court_no
			// this.getMemberList()
			this.getMyFriends()
		},
		onShow() {
			this.getMyFriends()
		},
		methods: {
			async shareFriend(){
				this.na_list = [];
				this.list_list = [];
				this.list.forEach((item) => {
					if (item.checked === true) {
						this.list_list.push(item.friend_phone);
						this.na_list.push(item.nums);
					}
				});
				let params = {
					project_id: getApp().globalData.projectId,
					open_id: uni.getStorageSync("openid"),
					app_id: this.$https.weixinAppId,
					phone: this.list_list.toString(),
					share_type: 'court',
					code: this.court_no,
					id: this.id,
					share_num: this.na_list.toString(),
				}
				console.log(params, '7878')
				// let res = await venues.shareFriend(params)
				// console.log(res, '1144')
				// if (res.code == 1) {
				// 	uni.navigateBack({
				// 		delta: 1, //返回层数，2则上上页
				// 	})
				// }
				// venues.shareFriend(params).then(res => {
				// 	console.log(res)
				// })
				console.log(this.na_list,'8899')
			},
			async getMyFriends() {
				let params = {
					project_id: getApp().globalData.projectId,
					open_id: uni.getStorageSync("openid"),
					app_id: this.$https.weixinAppId,
					id: this.id
				}
				let res = await venues.getMyFriends(params)
				if (res.code == 1) {
					this.list = res.data.result.data
					this.num = res.data.result.num
					// console.log(this.num, '4')
			
					this.list.forEach((exa, idx) => {
						// console.log(exa.chosen, '222111')
						if (this.num <= 0) {
							// console.log(1212)
							exa.disabled = true
			
						}
						if (exa.chosen == true) {
							exa.disabled = true
			
						}
					})
			
				}
			},
			addlist() {
				console.log(123132)
				uni.navigateTo({
					url: '../../../../pages_other/teammate/add/add'
				});
			},
			goshopping() {
				uni.navigateTo({
					url: '../contact/contact'
				})
			},
			change(e) {
				console.log(e)
			},
			selected(item) {
				console.log(item,'1122')
				
				item.checked = !item.checked
				if (!item.checked) {
					this.allchecked = false

				} else {
					const a = this.list.forEach((item) => {
						return item.checked === true
					})
					if (a) {
						this.allchecked = true
					} else {
						this.allchecked = false
					}
				}
			},
			selectgoods() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.list.map(item => {
						this.checked = true
						item.checked = true
					})
				} else {
					this.checked = false
					this.list.map(item => {
						item.checked = false
					})
				}
			},
			reduce(item) {
				let nums = item.nums
				if (nums > 1) {
					nums -= 1
				} else if (nums = 1) {
					uni.showToast({
						title: "该宝贝不能减少了哟~"
					})
				}


				item.nums = nums
				console.log(item.nums, '1111')
			},
			add(item) {
				let nums = item.nums
				item.nums = nums + 1
				console.log(item.nums, '2222')
			}
		},

	}
</script>

<style lang="scss">
	.goods {
		line-height: 80rpx;
		background-color: #FFFFFF;

		&-detail {
			display: flex;
			padding: 30rpx 15rpx 30rpx 30rpx;
			background-color: #fff;
			justify-content: space-between;
			border-bottom: 5rpx solid #F1F1F1;
			align-items: center;

			.detail-left {
				display: flex;

				.goods-left {
					display: flex;
					align-items: center;
				}
			}

			.size {
				display: flex;
				justify-content: space-around;
				flex-direction: column;
				margin-left: 30rpx;

				.goods-price {
					font-size: 25rpx;
					color: #F44545;

				}
			}

			.detail-right {
				text {
					width: 50rpx;
					line-height: 50rpx;
					text-align: center;
					display: inline-block;
					background-color: #F7F7F7;
					margin-right: 10rpx;
				}

				.add {
					color: #FA4305;
					border-radius: 10rpx 30rpx 30rpx 10rpx;
					margin-right: 20rpx;
				}

				.subtract {
					border-radius: 30rpx 10rpx 10rpx 30rpx;
				}
			}
		}
	}

	.group_5 {
		margin-left: 30rpx;
		line-height: 23rpx;
	}



	.text_2 {
		color: #999999;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 23rpx;
	}

	.text_3 {
		color: #2f75fa;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 22rpx;
	}

	.text_4 {
		color: #999999;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 23rpx;
	}

	.empty {

		position: relative;
		top: 220rpx;
		text-align: center;
		display: flex;
		align-items: center;
		flex-direction: column;

		&-text {
			color: #808080;
			margin-bottom: 50rpx;
		}

		&-button {
			width: 300rpx;
			height: 90rpx;
			color: orange;
			border: 1rpx solid orange;
			text-align: center;
			line-height: 90rpx;
			border-radius: 48rpx;
		}
	}

	.end {
		width: 100%;
		height: 90rpx;
		background-color: #fff;
		position: fixed;
		bottom: 100rpx;
		left: 0;
		display: flex;
		align-items: center;

		&-left {
			width: 70%;
			display: flex;
			justify-content: space-between;
			padding: 0 30rpx;

			.end-flex {
				display: flex;
				align-items: center;
			}
		}

		&-right {
			width: 30%;
			line-height: 90rpx;
			background-color: #F44545;
			text-align: center;
			color: #fff;
		}
	}
</style>
